﻿<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT language="c#"  runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    System.IO.DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("~/UpFiles/写真"));
    if (directoryInfo.Exists)
    {
        DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
        if( directoryInfos.Count()>0){
            foreach (var directory in directoryInfos)
            {
                FileInfo[] fileInfos = directory.GetFiles();
                if (fileInfos.Count() > 0)
                {
                    foreach (FileInfo fileInfo in fileInfos)
                    {
                        if (fileInfo.Extension.ToLower() == ".jpg" || fileInfo.Extension.ToLower() == ".gif" || fileInfo.Extension.ToLower() == ".png")
                        {
                            string fileName = fileInfo.FullName;
                            string imgName =
                                Server.MapPath("~/UpFiles/写真/" + directory.Name + "/Thumbnail/" + fileInfo.Name);
                            MakeThumbnail(fileName,imgName,150,150,"Cut");
                        }

                    }
                }
            }
           
        }
        this.Label1.Text = "重新生成完成！！";
    }
}

/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径（物理路径）</param>
/// <param name="thumbnailPath">缩略图路径（物理路径）</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
    int towidth = width;
    int toheight = height;
    int x = 0;
    int y = 0;
    int ow = originalImage.Width;
    int oh = originalImage.Height;
    switch (mode)
    {
        case "HW"://指定高宽缩放（可能变形）
            break;
        case "W"://指定宽，高按比例
            toheight = originalImage.Height * width / originalImage.Width;
            break;
        case "H"://指定高，宽按比例
            towidth = originalImage.Width * height / originalImage.Height;
            break;
        case "Cut"://指定高宽裁减（不变形）
            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
            {
                oh = originalImage.Height;
                ow = originalImage.Height * towidth / toheight;
                y = 0;
                x = (originalImage.Width - ow) / 2;
            }
            else
            {
                ow = originalImage.Width;
                oh = originalImage.Width * height / towidth;
                x = 0;
                y = (originalImage.Height - oh) / 2;
            }
            break;
        default:
            break;
    }
    //新建一个bmp图片
    System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    //新建一个画板
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    //设置高质量插值法
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //设置高质量,低速度呈现平滑程度
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
    g.Clear(System.Drawing.Color.Transparent);
    //在指定位置并且按指定大小绘制原图片的指定部分
    g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
    new System.Drawing.Rectangle(x, y, ow, oh),
    System.Drawing.GraphicsUnit.Pixel);
    try
    {
        //以jpg格式保存缩略图
        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (System.Exception e)
    {
        throw e;
    }
    finally
    {
        originalImage.Dispose();
        bitmap.Dispose();
        g.Dispose();
    }
} 

</SCRIPT> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        OK
       相册数量：<span id="count"></span>
       相片数量：<span id="count1"></span>
       <div ><ul id="container"></ul></div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </div>
    </form>
    <script type="text/javascript">
        var dwr = {};
        dwr.engine={};
        dwr.engine._remoteHandleCallback = function (id, type, data) {
            //alert(data.cacheFileUrl);
            $("#count").text(data.albumCount);
            $("#count1").text(data.photoCount);
            $.ajax({ type: "GET",
                dataType: "script",
                url: "http://" + data.cacheFileUrl,
                scriptCharset: "GBK",
                success: function () {
                    for (var i = g_a$126573308d.length - 1; 0 < i; i--) {
                        var name = g_a$126573308d[i].name;
                        var largeUrl = g_a$126573308d[i].lurl.split("\/");
                        var imgServer = "http://img" + largeUrl[0] + ".ph.126.net/" + largeUrl[1] + "/" + largeUrl[2];
                        $("<li><a href='javascript:void(0)' onclick=\"GetPictures(\'"+g_a$126573308d[i].purl+"\',"+g_a$126573308d[i].id+")\"><img src='" + imgServer + "'></a><span>" + name + "</span></li>").appendTo($("#container"));
                    }
                }
            });
        };

        function GetPictures(url,id) {
            $("#container").empty();
            $.ajax({ type: "GET",
                dataType: "script",
                url: "http://" + url,
                scriptCharset: "GBK",
                success: function () {
                    var testId = eval("g_p$" + id + "d");
                    for (var i = testId.length - 1; 0 < i; i--) {
                        var name = testId[i].desc;
                        var smallUrl = testId[i].turl.split("\/");
                        var largeUrl = testId[i].ourl.split("\/");
                        var imgServer = "http://img" + smallUrl[0] + ".ph.126.net/" + smallUrl[1] + "/" + smallUrl[2];
                        var linkServer = "http://img" + largeUrl[0] + ".ph.126.net/" + largeUrl[1] + "/" + largeUrl[2];
                        $("<li><a href='"+linkServer+"')\"><img src='" + imgServer + "'></a><span>" + name + "</span></li>").appendTo($("#container"));
                    }
                }
            });
        
        }

        $(document).ready(function () {


//            $.post("http://photo.163.com/photo/gurenfangsheying/dwr/call/plaincall/UserSpaceBean.getUserSpace.dwr", { 'callCount': 1, 'scriptSessionId': "${scriptSessionId}187", 'c0-scriptName': "UserSpaceBean",
//                'c0-methodName': "getUserSpace", 'c0-id': 0, 'c0-param0': "string:gurenfangsheying", 'batchId': 102076
//            }, function (data) {
//                //eval(data);
//            });

        })
        
    </script>
</body>
</html>
